﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Platformer
{
    class Message
    {


        public static SpriteFont MESSAGE_FONT;

        public const int MAX_HORIZONTAL_SIZE = 500;
        public const int MAX_NUM_LINES_PER_SCREEN = 4;
        public const int LINE_SPACING_PIXELS = 20;

        public string SenderName { get; set; }
        public string SenderIcon { get; set; } //So you can see who is talking

        public String Body
        {
            set
            {
                SplitMessage = PackLinesIntoScreens(BestSplitString(value));
            }
        }
        public ArrayList SplitMessage { private set;  get; }


        private int _currentScreenIndex;
        public ArrayList CurrentScreen
        {
            get
            {
                return (ArrayList)SplitMessage[_currentScreenIndex];
            }
        }
        public bool FirstScreen { get { return _currentScreenIndex == 0; } }
        public bool LastScreen { get { return _currentScreenIndex == SplitMessage.Count - 1; } }


        public Message(string name, string text)
        {
            this.SenderName = name;
            this.Body = text;
        }

        /*
         *Decrements to the previous screen if possible. Returns true if successful, false otherwise 
         */
        public bool PreviousScreen()
        {
            if (_currentScreenIndex == 0) return false;
            else
            {
                _currentScreenIndex--;
                return true;
            }
        }

        /*
         *Increments to the next screen if possible. Returns true if successful, false otherwise 
         */
        public bool NextScreen()
        {
            if (_currentScreenIndex == SplitMessage.Count - 1) return false;
            else
            {
                _currentScreenIndex++;
                return true;
            }
        }

        private ArrayList BestSplitString(string text)
        {
            string[] words = text.Split(' ');

            ArrayList tokens = new ArrayList();

            StringBuilder aToken = new StringBuilder();

            float currentLength = 0;
            foreach (string word in words)
            {
                float wordLength = MESSAGE_FONT.MeasureString(word).X;
                if (currentLength + wordLength > MAX_HORIZONTAL_SIZE) //This is a break point
                {
                    tokens.Add(aToken.ToString());
                    aToken.Clear();
                    currentLength = 0;
                }
                currentLength += wordLength;
                aToken.Append(word + " ");
            }
            tokens.Add(aToken.ToString());
            return tokens;
        }

        private ArrayList PackLinesIntoScreens(ArrayList bestSplitStrings)
        {
            ArrayList result = new ArrayList();

            for (int i = 0; i < bestSplitStrings.Count; i += MAX_NUM_LINES_PER_SCREEN)
            {
                int count = (int)MathHelper.Clamp(bestSplitStrings.Count - i, 0, MAX_NUM_LINES_PER_SCREEN);
                result.Add(bestSplitStrings.GetRange(i,count));
            }

            return result;
        }
    }
}
